package tuiLayer;
import controllerLayer.*;
import modelLayer.*;

import java.util.*;

public class LoanUI {
    private static LoanCTR loanCTR ;
    private static PersonCTR personCTR ;
     private static DvdCTR dvdCTR ; 
    private static Scanner scanner = new Scanner(System.in);
    
    public static void menu(PersonCTR personCtr, DvdCTR dvdCtr, LoanCTR loanCtr){
        loanCTR = loanCtr;
        personCTR = personCtr;
        dvdCTR = dvdCtr;
    	boolean quit = false;
        while(!quit){
            printOptions();
            int input = prompt("Make your choice!");
            switch(input) {
                case 1:
                    createLoan();
                    break;
                case 2:
                    int id = prompt("\nEnter the Loan's id: ");
                    if(loanCTR.findLoan(id)!=null)
                    loanCTR.printLoan(id);
                    else
                    System.out.println("Enter a valid id");
                    break;
                case 3:
                    loanCTR.printLoans();
                    break;
                case 4:
                    id = prompt("\nEnter the friend's id: ");
                    if(loanCTR.findLoan(id)!=null)
                    loanCTR	.delete(id);
                    else
                     System.out.println("Enter a valid id");
                    break;
                case 5:
                    quit = true;
                    break;
                default:
                    System.out.println("Sorry, but that's not a choice!");
                    printOptions();
                
            }
        }
    }
    
    private static void printOptions(){
        System.out.println("***LoanMenu***");
        System.out.println("1. Add Loan");
        System.out.println("2. Show Loan");
        System.out.println("3. Show all loans");
        System.out.println("4. End Loan");
        System.out.println("5. Back to MainMenu");
    }
    
    private static int prompt(String prompt){
        System.out.print(" " + prompt + " ");
        int input = scanner.nextInt();
        return input;
    }
    
    private static String prompt2(String prompt) {
        System.out.print("  " + prompt + " ");
        String input = scanner.next();
        return input.toLowerCase();
    }
    
    
    
    /**
     * It creates and add the specific friend at the same time
     */
    private static void createLoan(){
        String borrowDate,state;
        
        int id, barCode, idPerson,period;
        
        borrowDate=prompt2("Enter the date:");
        period=prompt("Enter the period:");
        state=prompt2("Enter the state:");
        
        idPerson=prompt("Enter the person's id");
        if(personCTR.findPerson(idPerson)!=null)
        {
			barCode=prompt("Enter the barcode");
			if(dvdCTR.findDvd(barCode)!=null)
			{
				Person person=personCTR.findPerson(idPerson);
				DVD dvd = dvdCTR.findDvd(barCode);
				Copy copy=dvdCTR.findDvd(barCode).checkCopy();
				if(copy!=null)
					loanCTR.createLoan(0,borrowDate,period,state,person,copy,dvd);
				else
					System.out.println("Not available copys");
			}
			else
				System.out.println("Not a valid barcode");
		}
		else
			System.out.println("Not a valid id"); 
    }
    
}
